home *** CD-ROM | disk | FTP | other *** search
- Path: solon.com!not-for-mail
- From: news@Thinkage.On.CA
- Newsgroups: comp.lang.c,comp.lang.c.moderated,hp.unix,comp.sys.hp.apps,comp.sys.hp.hpux
- Subject: Re: C coding problem
- Date: 4 Apr 1996 18:46:27 -0600
- Organization: Thinkage Ltd.
- Sender: clc@solutions.solon.com
- Approved: clc@solutions.solon.com
- Message-ID: <4k1qh3$5hn@solutions.solon.com>
- References: <4j06na$808@solutions.solon.com> <4jttan$3gf@solutions.solon.com> <4jv6st$crf@solutions.solon.com>
- NNTP-Posting-Host: solutions.solon.com
-
- In article <4jv6st$crf@solutions.solon.com> schwarz@mips.complang.tuwien.ac.at (Konrad Schwarz) writes:
- >
- >|> a[i] is converted to *(a + i) by the compiler in any case so
- >|> whats the advantage with your approach?
- >
- >The machine doesn't have to add, the machine doesn't have to multiply,
- >*a is less complicated to understand than a [i] since only one variable
- >is involved, *a is more type safe than a [i], since there is nothing
- >constraining i (besides it being of integral type), the optimizer
- >has less to do, compile time decreases, etc.
-
- However it is not always so that the machine does a distinct
- add or multiply in order to handle a[i]. The "*p++" idiom may look
- shot in the C code, and on a PDP-11 it usually resulted in nice
- short code. However, there is no guarentee that this is true of all
- hardware. It is not all that uncommon to have an architecture where
- adding 1 to an int is much cheaper than adding one to a pointer,
- In such and environment,
- *q++ = *p++;
- loses badly to
- q[i] = p[i], ++i;
- if the hardware indexing works in terms of the objects being
- referenced.
-
- >Why use C if you're uncomfortable with pointer arithmetic?
-
- I don't think the original poster was uncomfortable with pointers.
-
- There is a persistent folklore that says just because you code
- something with pointers it is magically faster. It is not true.
- Sometimes using a pointers is faster, sometimes using subscripting is
- faster. Usually it makes MINISCULE difference.
-
- The converse folklore is that subscripting is always more readable.
- It is also untrue. Pointer notation can greatly reduce program
- clutter so you can see what is happening.
-